<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="操作地址" prop="operIp">
            <el-input
              v-model="queryParams.operIp"
              placeholder="请输入操作地址"
              clearable
              style="width: 240px;"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="系统模块" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入系统模块"
              clearable
              style="width: 240px;"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="操作人员" prop="operName">
            <el-input
              v-model="queryParams.operName"
              placeholder="请输入操作人员"
              clearable
              style="width: 240px;"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="类型" prop="businessType">
            <el-select
              v-model="queryParams.businessType"
              placeholder="操作类型"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_oper_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="操作状态"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_common_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="操作时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" class="search-btn" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" class="reset-btn" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              class="action-button delete-btn"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['monitor:operlog:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              class="action-button delete-btn"
              @click="handleClean"
              v-hasPermi="['monitor:operlog:remove']"
            >清空</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              class="action-button export-btn"
              @click="handleExport"
              v-hasPermi="['monitor:operlog:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table 
          ref="tables" 
          v-loading="loading" 
          :data="list" 
          @selection-change="handleSelectionChange" 
          :default-sort="defaultSort" 
          @sort-change="handleSortChange"
          class="user-table"
          border
          stripe
          :header-cell-style="{background:'#f5f7fa', color:'#606266', height: '50px'}"
          :cell-style="{padding: '8px 0'}"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="日志编号" align="center" prop="operId" />
          <el-table-column label="系统模块" align="center" prop="title" :show-overflow-tooltip="true" />
          <el-table-column label="操作类型" align="center" prop="businessType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_oper_type" :value="scope.row.businessType"/>
            </template>
          </el-table-column>
          <el-table-column label="操作人员" align="center" prop="operName" width="110" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']" />
          <el-table-column label="操作地址" align="center" prop="operIp" width="130" :show-overflow-tooltip="true" />
          <el-table-column label="操作地点" align="center" prop="operLocation" :show-overflow-tooltip="true" />
          <el-table-column label="操作状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="操作日期" align="center" prop="operTime" width="160" sortable="custom" :sort-orders="['descending', 'ascending']">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.operTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="消耗时间" align="center" prop="costTime" width="110" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']">
            <template slot-scope="scope">
              <span>{{ scope.row.costTime }}毫秒</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="operation-column" width="120">
            <template slot-scope="scope">
              <div class="table-operation full-width">
                <a href="javascript:;" class="link-text view-link" @click="handleView(scope.row)" v-hasPermi="['monitor:operlog:query']">
                  <i class="el-icon-view"></i> 详细
                </a>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 操作日志详细 -->
    <el-dialog title="操作日志详细" :visible.sync="open" width="700px" append-to-body class="detail-dialog">
      <el-form ref="form" :model="form" label-width="100px" size="mini" class="detail-form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：">{{ form.title }} / {{ typeFormat(form) }}</el-form-item>
            <el-form-item label="登录信息：">{{ form.operName }} / {{ form.operIp }} / {{ form.operLocation }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求方式：">{{ form.requestMethod }}</el-form-item>
            <el-form-item label="操作状态：">
              <div v-if="form.status === 0">正常</div>
              <div v-else-if="form.status === 1">失败</div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作时间：">{{ parseTime(form.operTime) }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作URL：">{{ form.operUrl }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数：">{{ form.operParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数：">{{ form.jsonResult }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="错误消息：">{{ form.errorMsg }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="dialog-btn cancel-btn" @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { list, delOperlog, cleanOperlog } from "@/api/monitor/operlog";

  export default {
    name: "Operlog",
    dicts: ['sys_oper_type', 'sys_common_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        list: [],
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 默认排序
        defaultSort: {prop: 'operTime', order: 'descending'},
        // 表单参数
        form: {},
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          operIp: undefined,
          title: undefined,
          operName: undefined,
          businessType: undefined,
          status: undefined
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询登录日志 */
      getList() {
        this.loading = true;
        list(this.addDateRange(this.queryParams, this.dateRange)).then( response => {
            this.list = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      // 操作日志类型字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.dict.type.sys_oper_type, row.businessType);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.queryParams.pageNum = 1;
        this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      },
      /** 多选框选中数据 */
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.operId)
        this.multiple = !selection.length
      },
      /** 排序触发事件 */
      handleSortChange(column, prop, order) {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
        this.getList();
      },
      /** 详细按钮操作 */
      handleView(row) {
        this.open = true;
        this.form = row;
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const operIds = row.operId || this.ids;
        this.$modal.confirm('是否确认删除日志编号为"' + operIds + '"的数据项？').then(function() {
          return delOperlog(operIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 清空按钮操作 */
      handleClean() {
        this.$modal.confirm('是否确认清空所有操作日志数据项？').then(function() {
          return cleanOperlog();
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('monitor/operlog/export', {
          ...this.queryParams
        }, `operlog_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

<style lang="scss" scoped>
// 整体容器样式优化 - 更加商务化的设计
.app-container {
  background-color: #fafbfc;
  border-radius: 4px;
  padding: 24px;
  box-shadow: 0 1px 8px rgba(0, 0, 0, 0.03);
}

// 搜索区域优化 - 简洁商务风格
::v-deep .el-form {
  background-color: #fff;
  padding: 24px 20px;
  border-radius: 4px;
  margin-bottom: 24px;
  border: 1px solid #ebeef5;

  .el-form-item {
    margin-bottom: 16px;
    margin-right: 20px;
    
    &:last-child {
      margin-right: 0;
      margin-bottom: 0;
    }
    
    .el-form-item__label {
      padding-right: 12px;
      color: #606266;
      font-weight: 400;
      line-height: 36px;
    }
  }

  .el-input__inner {
    border-radius: 3px;
    transition: all 0.2s;
    height: 36px;
    line-height: 36px;
    
    &:hover {
      border-color: #c0c4cc;
    }
    
    &:focus {
      border-color: #0960bd;
    }
  }
  
  .el-date-editor {
    height: 36px;
    line-height: 36px;
    
    .el-range-separator {
      line-height: 24px;
    }
  }
  
  .el-select {
    width: 100%;
    
    .el-input {
      height: 36px;
      line-height: 36px;
    }
  }
}

// 搜索和重置按钮样式与间距
.search-btn {
  background-color: #0960bd;
  border: 1px solid #0960bd;
  color: #fff;
  padding: 8px 18px;
  border-radius: 3px;
  font-size: 12px;
  margin-right: 10px;
  height: 36px;

  &:hover, &:focus {
    background-color: #0854a9;
    border-color: #0854a9;
  }
}

.reset-btn {
  border-radius: 3px;
  padding: 8px 18px;
  border: 1px solid #dcdfe6;
  background-color: #f2f3f5;
  color: #606266;
  font-size: 12px;
  height: 36px;

  &:hover, &:focus {
    background-color: #e9ebef;
    color: #333;
  }
}

// 按钮区域优化 - 更加商务化的布局
.mb8 {
  margin-bottom: 24px;
  display: flex;
  align-items: center;
  background-color: #fff;
  padding: 16px;
  border-radius: 4px;
  border: 1px solid #ebeef5;
}

// 表格样式优化 - 商务风格
.user-table {
  margin-top: 24px;
  border-radius: 4px;
  overflow: hidden;
  border: 1px solid #ebeef5;

  ::v-deep .el-table {
    border-radius: 4px;

    &::before {
      display: none;
    }

    th {
      background-color: #f2f6fc !important;
      font-weight: 500;
      color: #333;
      padding: 16px 0;
      border-bottom: 1px solid #ebeef5;
      height: 56px;
    }

    td {
      padding: 14px 0;
      border-bottom: 1px solid #ebeef5;
      height: 54px;
    }
  }

  ::v-deep .el-table__row {
    transition: background-color 0.2s;
    
    &:hover > td {
      background-color: #f5f7fa;
    }
  }
}

// 顶部操作按钮组样式 - 商务风格
.action-button {
  font-weight: 400;
  border-radius: 3px;
  transition: all 0.2s;
  padding: 8px 18px;
  margin-right: 16px;
  font-size: 12px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  height: 36px;
  
  i {
    margin-right: 4px;
  }
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  }
  
  &:active {
    transform: translateY(0);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  }
  
  &[disabled] {
    opacity: 0.7;
    &:hover {
      transform: none;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }
  }
}

// 删除按钮样式
.delete-btn {
  background-color: #d03050;
  border: 1px solid #d03050;
  color: #fff;
  
  &:hover {
    background-color: #e16b84;
    border-color: #e16b84;
  }
}

// 导出按钮样式
.export-btn {
  background-color: #E6A23C;
  border: 1px solid #E6A23C;
  color: #fff;
  
  &:hover {
    background-color: #ebb563;
    border-color: #ebb563;
  }
}

// 表格操作區域優化 - 商務風格鏈接
.table-operation {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: nowrap;
  white-space: nowrap;

  &.full-width {
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
  }

  .link-text {
    font-size: 13px;
    text-decoration: none;
    cursor: pointer;
    padding: 2px 4px;
    transition: all 0.2s;

    i {
      margin-right: 2px;
      font-size: 12px;
    }

    &:hover {
      text-decoration: underline;
    }
  }

  .view-link {
    color: #409EFF;

    &:hover {
      color: #66b1ff;
    }
  }
}

// 分页样式优化 - 去除白色背景框
::v-deep .pagination-container {
  margin-top: 24px;
  display: flex;
  justify-content: flex-end;
  padding: 10px 0;
  background-color: transparent;
  border-radius: 0;
  border: none;
  box-shadow: none;
  overflow: visible;
}

::v-deep .el-pagination {
  padding: 0;
  font-weight: normal;
  
  .btn-prev, .btn-next {
    border: 1px solid #dcdfe6;
    border-radius: 3px;
    padding: 0 8px;
    margin: 0 5px;
    background-color: #fff;
    
    &:hover {
      color: #0960bd;
    }
  }
  
  .el-pager li {
    border: 1px solid #dcdfe6;
    border-radius: 3px;
    margin: 0 3px;
    
    &.active {
      background-color: #0960bd;
      color: #fff;
      border-color: #0960bd;
    }
    
    &:hover:not(.active) {
      color: #0960bd;
    }
  }
}

// 详情弹窗样式优化 - 商务风格
.detail-dialog {
  ::v-deep .el-dialog {
    border-radius: 4px;
    overflow: hidden;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);

    .el-dialog__header {
      border-bottom: 1px solid #ebeef5;
      padding: 16px 20px;
      background: #fafbfc;
    }

    .el-dialog__title {
      font-size: 16px;
      font-weight: 500;
      color: #303133;
    }

    .el-dialog__headerbtn {
      top: 16px;
    }

    .el-dialog__body {
      padding: 24px 24px 10px;
    }

    .el-dialog__footer {
      border-top: 1px solid #ebeef5;
      padding: 14px 20px;
    }
  }
}

.detail-form {
  ::v-deep .el-form-item {
    margin-bottom: 16px;
  }

  ::v-deep .el-form-item__label {
    color: #606266;
    font-weight: 500;
  }
}

// 对话框按钮样式
.dialog-btn {
  border-radius: 3px;
  padding: 8px 20px;
  font-weight: 400;
  font-size: 13px;
}

.cancel-btn {
  background-color: #f2f3f5;
  border-color: #dcdfe6;
  color: #606266;

  &:hover, &:focus {
    background-color: #e9ebef;
    color: #333;
  }
}
</style>
